package com.app.biz.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.app.common.annotation.Anonymous;
import com.app.common.core.page.TableDataInfo;
import com.app.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.app.common.annotation.Log;
import com.app.common.core.controller.BaseController;
import com.app.common.core.domain.AjaxResult;
import com.app.common.enums.BusinessType;
import com.app.biz.domain.BizSocialElectricityConsumption;
import com.app.biz.service.IBizSocialElectricityConsumptionService;


/**
 * 全社会用电量Controller
 * 
 * @author Zhou_S
 * @date 2023-12-28
 */
@RestController
@RequestMapping("/biz/socialElectricity")
public class BizSocialElectricityConsumptionController extends BaseController
{
    @Autowired
    private IBizSocialElectricityConsumptionService bizSocialElectricityConsumptionService;

    /**
     * 查询全社会用电量列表
     */
    @PreAuthorize("@ss.hasPermi('biz:socialElectricity:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizSocialElectricityConsumption bizSocialElectricityConsumption)
    {
        if(StringUtils.isNotBlank(bizSocialElectricityConsumption.getDates())){
            DateTime datesTimes= DateUtil.parse(bizSocialElectricityConsumption.getDates(),"yyyy年MM月");
            bizSocialElectricityConsumption.setDates(DateUtil.format(datesTimes,"yyyy-MM"));
        }else{
            throw new RuntimeException("未指定日期");
        }
        startPage();
        List<BizSocialElectricityConsumption> list = bizSocialElectricityConsumptionService.selectBizSocialElectricityConsumptionList(bizSocialElectricityConsumption);
        return getDataTable(list);
    }

    /**
     * 导出全社会用电量列表
     */
    @PreAuthorize("@ss.hasPermi('biz:socialElectricity:export')")
    @Log(title = "全社会用电量", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizSocialElectricityConsumption bizSocialElectricityConsumption)
    {
        List<BizSocialElectricityConsumption> list = bizSocialElectricityConsumptionService.selectBizSocialElectricityConsumptionList(bizSocialElectricityConsumption);
        ExcelUtil<BizSocialElectricityConsumption> util = new ExcelUtil<BizSocialElectricityConsumption>(BizSocialElectricityConsumption.class);
        util.exportExcel(response, list, "全社会用电量数据");
    }

    /**
     * 获取全社会用电量详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:socialElectricity:query')")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(bizSocialElectricityConsumptionService.selectBizSocialElectricityConsumptionByBaseId(baseId));
    }

    /**
     * 新增全社会用电量
     */
    @PreAuthorize("@ss.hasPermi('biz:socialElectricity:add')")
    @Log(title = "全社会用电量", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizSocialElectricityConsumption bizSocialElectricityConsumption)
    {
        return toAjax(bizSocialElectricityConsumptionService.insertBizSocialElectricityConsumption(bizSocialElectricityConsumption));
    }

    /**
     * 修改全社会用电量
     */
    @PreAuthorize("@ss.hasPermi('biz:socialElectricity:edit')")
    @Log(title = "全社会用电量", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizSocialElectricityConsumption bizSocialElectricityConsumption)
    {
        return toAjax(bizSocialElectricityConsumptionService.updateBizSocialElectricityConsumption(bizSocialElectricityConsumption));
    }

    /**
     * 删除全社会用电量
     */
    @PreAuthorize("@ss.hasPermi('biz:socialElectricity:remove')")
    @Log(title = "全社会用电量", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        return toAjax(bizSocialElectricityConsumptionService.deleteBizSocialElectricityConsumptionByBaseIds(baseIds));
    }

    /**
     * <p>
     *     查询APP左上角的日期数据
     * </p>
     * @return 日期的集合
     */
    @GetMapping("/getUniqueDatesFromDatabase")
    public AjaxResult getUniqueDatesFromDatabase(){
        List<String> dateArgs = bizSocialElectricityConsumptionService.getUniqueDatesFromDatabase();
        return AjaxResult.success(dateArgs);
    }

    /**
     * <p>
     *     清空所有的全社会用电量数据
     * </p>
     * @return
     */
    @DeleteMapping("/clearAllTableData")
    public AjaxResult clearAllTableData(){
        bizSocialElectricityConsumptionService.clearAllTableData();
        return AjaxResult.success();
    }

    /**
     * <p>
     *     将临时表中的数据迁移到正式表虽
     * </p>
     * @return
     */
    @Anonymous
    @PostMapping("/transferDataInOneClick")
    public AjaxResult transferDataInOneClick(){
        return toAjax(bizSocialElectricityConsumptionService.transferDataInOneClick());
    }
}
